package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Override
    public PageResultVo<WmMaterial> findPage(WmMaterialPageRequestDto dto) {

        //1.分页
        IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(),dto.getSize());

        //2.构建分页条件

        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();

        //2.1 素材必须是用户上传的，即 user_id 必须是用户Id
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());

        //2.2 是否收藏
        lqw.eq(null != dto.getIsCollection()&& dto.getIsCollection().floatValue() !=0,
                WmMaterial::getIsCollection,dto.getIsCollection());

        //2.3分页查询
        IPage<WmMaterial> page = page(pageInfo, lqw);

        //返回数据
        return PageResultVo.pageResult(pageInfo.getPages(),
                pageInfo.getSize(),page.getTotal(),page.getRecords());
    }
}
